package com.juzipi.demo.handler;

import com.juzipi.demo.constant.ResultCode;
import com.juzipi.demo.tools.json.ResultTool;
import com.juzipi.demo.utils.json.JsonAuthentication;
import com.juzipi.demo.utils.json.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功处理
 */

@Component
public class MyAuthenticationSuccessHandler extends JsonAuthentication implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        System.out.println("====================>>> 登陆成功处理执行");
        System.out.println("ip地址："+httpServletRequest.getRemoteAddr());
        User user = (User)authentication.getPrincipal();
        System.out.println("用户名："+user.getUsername());
        System.out.println("角色："+user.getAuthorities());

        Result result = ResultTool.successCustomize(ResultCode.LOGIN_SUCCESSFULLY, null);

        this.WriteJson(httpServletRequest,httpServletResponse,result);

    }
}
